#/usr/bin/python27
#-*- coding: utf-8 -*-
#  MS-SMS Test tool
#
#  Created by wuweihui@cn.ibm.com

import os, sys, re
import wx
import wx.aui
import threading
from SMSTestCase import Selector
try:
    from agw import customtreectrl as CT
except ImportError: # if it's not there locally, try the wxPython lib.
    import wx.lib.agw.customtreectrl as CT

    
class CaseTreeCtrl(CT.CustomTreeCtrl):

    def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition,
                 size=wx.DefaultSize,
                 style=wx.SUNKEN_BORDER|wx.WANTS_CHARS,
                 agwStyle=CT.TR_HAS_BUTTONS|CT.TR_HAS_VARIABLE_ROW_HEIGHT,
                 log=None):

        CT.CustomTreeCtrl.__init__(self, parent, id, pos, size, style, agwStyle)
               
        
        self.root = self.AddRoot("Case List", ct_type=1)
        self.SetPyData(self.root, None)
        
        self.Bind(CT.EVT_TREE_ITEM_CHECKED, self.OnCheckItem)
        
    def get_case_dict(self, parent=None):
        casedict = {}
        parent = parent or self.root
        (child, cookie) = self.GetFirstChild(parent)
        while child:
            if self.HasChildren(child):
                casedict.update(self.get_case_dict(child))
            elif child.IsChecked():
                k = parent.GetData() or parent.GetText()
                v = child.GetData() or child.GetText()
                if casedict.has_key(k):
                  casedict[k].append(v)
                else:
                  casedict[k] = [v]
            (child, cookie) = self.GetNextChild(parent, cookie)
        return casedict
     
    def create_tree(self, caselist):
      for case in caselist:
         cn = case.__name__
         caseitem = self.AppendItem(self.root, cn, ct_type=1)
         self.SetPyData(caseitem, case)
         methods = Selector.read_test_methods(case)
         for method in methods:
            methoditem = self.AppendItem(caseitem, method, ct_type=1)
            self.SetPyData(methoditem, None)
            
    def OnCheckItem(self, evt):
       item = evt.GetItem()
       #print dir(evt)
       self.CheckChilds(item, item.IsChecked())
       self.AutoCheckParent(item, item.IsChecked())
            
class TestCasePanel(wx.Panel):
   def __init__(self, parent, id=-1, frame=None):
      wx.Panel.__init__(self, parent, id)
      self.frame = frame
      sizer = wx.BoxSizer(wx.VERTICAL)
      
      self.treectrl = CaseTreeCtrl(self)
      sizer.Add(self.treectrl, 1, wx.EXPAND|wx.ALL, 0)
      self.SetSizer(sizer)
      
      self.treectrl.create_tree(Selector.read_test_cases('SMSTestCase'))
      
      button = wx.Button(self, -1, 'Show')
      sizer.Add(button, 0, wx.EXPAND|wx.ALL, 0)
      sizer.Layout()
      
      self.Bind(wx.EVT_BUTTON, self.OnButton, button)
      
   def OnButton(self, evt):
      for k, v in self.treectrl.get_case_dict().items():
         print k, v, type(k)
   
  
class TestFrame(wx.Frame):
   def __init__(self, parent, id=-1):
      wx.Frame.__init__(self, parent, id, 'SMS Test Tool Beta by wuweihui@cn.ibm.com', size = (970, 720))
      self.SetSize((220,380))
      
      self.panel = TestCasePanel(self)
      
            
            
class TestApp(wx.App):

   def OnInit(self):
      self.frame = TestFrame(None)
      self.frame.Show(True)
      return True
      
if __name__ == "__main__":
   app = TestApp(False)
   app.MainLoop()
   